package com.wwl.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wwl.springcloud.services.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author wwl
 * @version 1.0
 * @date 2021/8/10 19:11
 * @description:
 */
@RestController
@Slf4j
@Api(value = "/consumer",description = "hystrix测试", tags = {"consumer"})
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod") //指定全局默认fallback方法
public class PaymentHystrixController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @ApiOperation("hystrix-ok测试")
    @PostMapping("/payment/hystrix/ok")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    public String paymentInfo_OK(@ApiParam(value = "id") @RequestParam(value = "id",required = false) Integer id){
        String result = paymentService.getPaymentById(id);
        //int a = 10/0;
        log.info("*******result:"+result);
        return result;
    }
    @ApiOperation("hystrix-timeout测试")
    @PostMapping("/payment/hystrix/timeout")
   /* @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })*///单个降级方法
    @HystrixCommand //使默认方法生效
    public String paymentInfo_TimeOut(@ApiParam(value = "id") @RequestParam(value = "id",required = false) Integer id){
        String result = paymentService.paymentFeignTimeout(id);
        log.info("*******result:"+result);
        return result;
    }

    //兜底方法
    public String paymentTimeOutFallbackMethod(@ApiParam(value = "id") @RequestParam(value = "id",required = false) Integer id){
        return "我是消费者80，对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬＿┬)";
    }

    //全局默认fallback方法
    public String payment_Global_FallbackMethod(){
        return "Global异常处理信息，请稍后再试！！！";
    }


}
